from flask import Flask, request, jsonify
import io
import numpy as np
from Core import process_img

import cv2

app = Flask(__name__)

def convert_byte_array_to_opencv_image(byte_array):
    # 将字节流转换为numpy数组
    np_arr = np.frombuffer(byte_array, np.uint8)
    # 将numpy数组解码为图像
    img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
    # 检查图像是否成功读取
    return img

@app.route('/kernel', methods=['POST'])
def upload():
    file = request.files['file']
    try:
        img = convert_byte_array_to_opencv_image(
            io.BytesIO(file.read()).getvalue()
        )
        return jsonify(
            process_img(img)
        ), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == "__main__":
    app.run(host='0.0.0.0')